from typing import List, Optional
from pydantic import BaseModel
from datetime import datetime

from app.schemas.user import UserBrief

# 媒体基础模式
class PostMediaBase(BaseModel):
    media_type: str  # image 或 video
    url: str
    thumbnail_url: Optional[str] = None
    order: int = 0

# 创建媒体
class PostMediaCreate(PostMediaBase):
    pass

# 媒体信息
class PostMedia(PostMediaBase):
    id: int
    post_id: int
    created_at: datetime
    
    class Config:
        orm_mode = True

# 标签基础模式
class TagBase(BaseModel):
    name: str

# 创建标签
class TagCreate(TagBase):
    pass

# 标签信息
class Tag(TagBase):
    id: int
    created_at: datetime
    
    class Config:
        orm_mode = True

# 帖子基础模式
class PostBase(BaseModel):
    title: str
    content: str
    campsite_id: Optional[int] = None

# 创建帖子
class PostCreate(PostBase):
    media: Optional[List[PostMediaCreate]] = []
    tags: Optional[List[str]] = []

# 更新帖子
class PostUpdate(BaseModel):
    title: Optional[str] = None
    content: Optional[str] = None
    campsite_id: Optional[int] = None
    is_published: Optional[bool] = None

# 帖子简要信息
class PostBrief(BaseModel):
    id: int
    title: str
    author_id: int
    author: UserBrief
    view_count: int
    like_count: int
    comment_count: int
    created_at: datetime
    cover_media: Optional[PostMedia] = None  # 封面媒体（第一张图片或视频）
    
    class Config:
        orm_mode = True

# 完整帖子信息
class Post(PostBase):
    id: int
    author_id: int
    author: UserBrief
    is_published: bool
    is_deleted: bool
    view_count: int
    like_count: int
    comment_count: int
    created_at: datetime
    updated_at: Optional[datetime] = None
    media: List[PostMedia] = []
    tags: List[Tag] = []
    
    class Config:
        orm_mode = True

# 带有详细信息的帖子
class PostWithDetails(Post):
    # 扩展Post类，可以添加更多详细信息
    # 如推荐原因、相似度分数等
    similarity_score: Optional[float] = None
    recommendation_reason: Optional[str] = None
    
    class Config:
        orm_mode = True

# 评论基础模式
class CommentBase(BaseModel):
    content: str
    parent_id: Optional[int] = None

# 创建评论
class CommentCreate(CommentBase):
    pass

# 评论信息
class Comment(CommentBase):
    id: int
    post_id: int
    user_id: int
    user: UserBrief
    created_at: datetime
    updated_at: Optional[datetime] = None
    replies: List['Comment'] = []
    
    class Config:
        orm_mode = True 